package com.gxa.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.pojo.Article;
import com.gxa.dao.ArticleMapper;
import com.gxa.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public boolean addArticle(Article article) {

       return articleMapper.insertArticle(article)>0;
    }


    @Override
    public List<Article> showArticlesByPage(int pageNum,int pageSize) {

        PageHelper.startPage(pageNum,pageSize);

        List<Article> articles = articleMapper.selectAllArticles();

        return articles;
//        return articleMapper.selectAllArticles();
    }

    @Override
    public boolean deleteArticleByTitle(String title) {
        return articleMapper.deleteArticleByTitle(title)>0;
    }

    @Override
    public List<Article> selectArticleByTitleOrType(String title, String type) {
        return articleMapper.selectArticleByTitleOrType(title, type);
    }

    @Override
    public boolean batchDelete(String[] titles) {
        return articleMapper.batchDelete(titles)>0;
    }

    @Override
    public int getCount() {
        List<Article> articles = articleMapper.selectAllArticles();

        PageInfo<Article> pageInfo=new PageInfo<>(articles);

        int total =(int)pageInfo.getTotal();

        return total;
    }
}
